// pages/circleDetail/circleDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: {},
    comment:"",
    content: '',
    readcount: 1100,
    likecount: 10,
    starcount: 10,
    author_id: 0,
    imageList: '',
    ip: '',
    author_name: '',
    time: '',
    likeSign: 0,
    starSign: 0,
    user_id: 0,
    post_id: 0,
    user_id: ''
  },
  likeBtn: function (){
    
    var app = getApp()
    var ip = app.globalData.ServerAddress
    this.setData({
      user_id: app.globalData.user_id
    })
    let that = this
    console.log(this.data.likeSign)
    // if(this.data.likeSign==0){
      wx.request({
        url: "http://" + ip + "/post/like",
        method: 'POST',
        dataType: "json",
        header: {
          'Content-type': 'application/json',
          
          "authorization": app.globalData.token
        },
        data: {
          post_id: this.data.post_id,
          user_id: app.globalData.user_id
        },
        success: function (res) {
          console.log(res)
          // console.log(res)
          // that.setData({
          //   likeSign: 1
          // })
          console.log("dz")
          let p = {
            post_id: that.data.post_id,
            user_id: that.data.user_id
          }
          that.onLoad(p)
          // const pages = getCurrentPages()     //获取页面列表
          // const perpage = pages[pages.length - 1]    //当前页   
          // perpage.onLoad() 
        }
      })
      console.log(this.data.likeSign)
    // }else{
    //   wx.request({
    //     url: "http://" + ip + "/post/like",
    //     method: 'DELETE',
    //     dataType: "json",
    //     header: {
    //       'Content-type': 'application/json'
    //     },
    //     data: {
    //       post_id: this.data.post_id,
    //     },
    //     success: function (res) {
    //       // console.log(res)
    //       that.setData({
    //         likeSign: 0
    //       })
    //       console.log("dz")
    //       const pages = getCurrentPages()     //获取页面列表
    //       const perpage = pages[pages.length - 1]    //当前页   
    //       perpage.onLoad() 
    //     }
    //   })
    //   console.log(this.data.likeSign)
    // }
  },
  starBtn: function (){
    var app = getApp()
    var ip = app.globalData.ServerAddress
    let that = this
    console.log(this.data.likeSign)
    // if(this.data.starSign==0){
      wx.request({
        url: "http://" + ip + "/user/star",
        method: 'POST',
        dataType: "json",
        header: {
          'Content-type': 'application/json',
          
          "authorization": app.globalData.token
        },
        data: {
          user_id: app.globalData.user_id,
          post_id: this.data.post_id,
        },
        success: function (res) {
          // console.log(res)
          that.setData({
            starSign: 1
          })
          console.log(res)
          
          let p = {
            post_id: that.data.post_id,
            user_id: that.data.user_id
          }
          that.onLoad(p)
          // const pages = getCurrentPages()     //获取页面列表
          // const perpage = pages[pages.length - 1]    //当前页   
          // perpage.onLoad() 
        }
      })
      console.log(this.data.starSign)
    // }else{
    //   wx.request({
    //     url: "http://" + ip + "/post/like",
    //     method: 'DELETE',
    //     dataType: "json",
    //     header: {
    //       'Content-type': 'application/json'
    //     },
    //     data: {
    //       post_id: this.data.post_id,
    //     },
    //     success: function (res) {
    //       // console.log(res)
    //       that.setData({
    //         starSign: 0
    //       })
    //       console.log("dz")
    //       const pages = getCurrentPages()     //获取页面列表
    //       const perpage = pages[pages.length - 1]    //当前页   
    //       perpage.onLoad() 
    //     }
    //   })
    //   console.log(this.data.starSign)
    // }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      post_id : options.post_id,
      user_id : options.user_id
    })
    console.log(this.data.post_id)
    var app = getApp()
    var ip = app.globalData.ServerAddress
    console.log(ip)
    let that = this
    that.setData({
      ip: ip
    })
      wx.request({
        url: "http://" + ip + "/post/comments",
        method: 'GET',
        dataType: "json",
        header: {
          'Content-type': 'application/json',
          
        "authorization": app.globalData.token
        },
        data: {
          post_id: this.data.post_id,
          page: 1,
          per_page: 5
        },
        success: function (res) {
          // console.log(res)
          that.setData({
            list: res.data.comment,
          })
        }
      }),
      wx.request({
        url: "http://" + ip + "/post/option",
        method: 'GET',
        dataType: "json",
        header: {
          'Content-type': 'application/json',
          'authorization': app.globalData.token
        },
        data: {
          id: this.data.post_id
        },
        success: function (res) {
          console.log(res)
          that.setData({
            content: res.data.content,
            readcount: res.data.read_count,
            likecount: res.data.like,
            starcount: res.data.star,
            author_id: res.data.author_id,
            imageList: res.data.medias,
            author_name: res.data.author,
            time: res.data.create_time
          })
        }
      })
  },
  
  postComment:function(e){
    var app = getApp()
    var ip = app.globalData.ServerAddress
    const that = this;
    let first
    first = e.detail.value.name
    console.log(e.detail.value.name)
    that.setData({
      first: e.detail.value,
      comment: first
    }),
    console.log(first)
    this.data.comment = first
    wx.request({
      url: "http://" + ip + "/post/comments",
      method: 'POST',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        
        "authorization": app.globalData.token
      },
      data: {
          "post_id": this.data.post_id,
          "author_id": app.globalData.user_id,
          "content": first
      },
      success: function (res) {
        console.log("成功")
        console.log(res)
        // const pages = getCurrentPages()     //获取页面列表
        // const perpage = pages[pages.length - 1]    //当前页   
        // perpage.onLoad() 
        // wx.reLaunch({
        //   url: '../circleDetail/circleDetail?post_id='+that.data.post_id+'&user_id='+app.globalData.user_id,
        // })
        let p = {
          post_id : that.data.post_id,
          user_id : that.data.user_id
        }
        that.onLoad(p)
      },
      fail: function(){
        console.log(123)
      }
    })
    console.log(first)
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})